package com.neusoft.neuiotms.common.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.common.dto.DictionaryTypeDTO;
import com.neusoft.neuiotms.common.model.DictionaryType;
import com.neusoft.neuiotms.common.repository.DictionaryTypeRepository;

@Component
public class DictionaryTypeConverter extends AbstractConverter<DictionaryType, DictionaryTypeDTO> {

    @Autowired
    private DictionaryTypeRepository dictionaryDescRepository;

    @Override
    public DictionaryType toModel(final DictionaryTypeDTO dto) {
        DictionaryType model;
        if (dto.isNew()) {
            model = new DictionaryType();
        } else {
            model = this.dictionaryDescRepository.getOne(dto.getId());
        }

        model.setText(dto.getText());
        model.setType(dto.getType());
        return model;
    }

    @Override
    public DictionaryTypeDTO toDTO(final DictionaryType model, final boolean forListView) {

        final DictionaryTypeDTO dto = new DictionaryTypeDTO();

        dto.setId(model.getId());
        dto.setText(model.getText());
        dto.setType(model.getType());

        return dto;
    }

}
